<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | create by allen.qiang
// +----------------------------------------------------------------------


/**
 * 商品推荐
 * @package  htdocs
 * @subpackage  quanliadmin
 * @create by allen.qiang
 */
require 'module/base/promotionHandler.class.php';
require 'module/base/goodsHandler.class.php';

class pa_goodsRecommend {

    public function execute() {
        global $G_PGLOB ;
        if (isset($_REQUEST['operFlg'])) {

            switch($_REQUEST['operFlg']) {

                case 1: //商品推荐信息列表
                    $this->goodsRecommendList();
                    break;

                case 2: //添加商品推荐信息
                    $this->goodsRecommendAdd();
                    break;

                case 3: //编辑商品推荐信息
                    $this->goodsRecommendEdit();
                    break;

                case 4: //商品推荐详情
                    $this->goodsRecommendDetail();
                    break;

                default:
                    $G_PGLOB->smarty->assign('msg', '参数错误，请审核后再操作');
                    $G_PGLOB->smarty->display('comm/error.tpl');

            }
        }
    }

    /**
     * 信息列表
     *
     */
    private function goodsRecommendList() {

        global $G_PGLOB;

        $G_PGLOB->smarty->display('promotion/goodsRecommendList.tpl');

    }

    /**
     * 添加信息
     */
    private function goodsRecommendAdd () {

        global $G_PGLOB;

        $G_PGLOB->smarty->assign('operFlg', 2);
        $G_PGLOB->smarty->display('promotion/goodsRecommendAdd.tpl');

    }

    /**
     * 编辑信息
     */
    public function goodsRecommendEdit() {

        global $G_PGLOB;

        if (!isset($_REQUEST['id']) || !is_numeric($_REQUEST['id'])) {
            $G_PGLOB->smarty->assign('msg', '参数错误');
            $G_PGLOB->smarty->display('comm/error.tpl');
            die() ;
        }

        //查询信息
        $goodsRecommendInfo = new entity('promotion_recom');
        $goodsRecommendInfo->recom_id = $_REQUEST['id'];

        if( !$G_PGLOB->DBCA->fetch($goodsRecommendInfo) ){
            $G_PGLOB->smarty->assign('msg', '没有找到信息');
            $G_PGLOB->smarty->display('comm/error.tpl');
            die() ;
        }

        $goodsHandler = new goodsHandler($G_PGLOB->DBCA);

        //查询主商品信息
        $main_array = array(	'product_code' 	=> $goodsRecommendInfo->product_code);
        $result_search = $goodsHandler->searchGoodsSale($main_array);

        $goodsRecommendInfo->goodsName = $result_search['goodsName'];
        $goodsRecommendInfo->salePrice = $result_search['salePrice'];

        $goodsRecommendInfo->uid = $result_search['postID'];

        $params['recom_id'] = $_REQUEST['id'];
        
        //实例化促销类
        $promotionHandler = new promotionHandler( $G_PGLOB->DBCA );

        $recommendDetail = $promotionHandler->searchGoodsRecommendDetail( $params );

        //组装详情数组
        $detailArr = array();

        if( $recommendDetail ){

            foreach ( $recommendDetail as $detail ){

                //创建临时数组
                $tempArr = array();

                $arg_array = array(	'product_code' 	=> $detail['product_code']);
                $result_search = $goodsHandler->searchGoodsSale($arg_array);

                $tempArr['product_code'] = (string)$detail['product_code'];
                $tempArr['goodsName']    = $result_search['goodsName'];
                $tempArr['salePrice']    = $result_search['salePrice'];
                $tempArr['sale_price']    = $detail['sale_price'];
                $tempArr['uid']	= $result_search['postID'];

                $detailArr[] = $tempArr;

            }

        }

        $G_PGLOB->smarty->assign_by_ref( 'recommendGoodsInfo' , $detailArr );
        $G_PGLOB->smarty->assign_by_ref('goodsRecommendInfo' , $goodsRecommendInfo);
        $G_PGLOB->smarty->assign('operFlg', 3);
        $G_PGLOB->smarty->display('promotion/goodsRecommendAdd.tpl');

    }

    /**
     * 详情
     */
    public function goodsRecommendDetail() {

        global $G_PGLOB;

        if (!isset($_REQUEST['id']) || !is_numeric($_REQUEST['id'])) {
            $G_PGLOB->smarty->assign('msg', '参数错误');
            $G_PGLOB->smarty->display('comm/error.tpl');
            die() ;
        }

        //查询信息
        $goodsRecommendInfo = new entity('promotion_recom');
        $goodsRecommendInfo->recom_id = $_REQUEST['id'];

        if( !$G_PGLOB->DBCA->fetch($goodsRecommendInfo) ){
            $G_PGLOB->smarty->assign('msg', '没有找到信息');
            $G_PGLOB->smarty->display('comm/error.tpl');
            die() ;
        }

        $goodsHandler = new goodsHandler($G_PGLOB->DBCA);

        //查询主商品信息
        $main_array = array(	'product_code' 	=> $goodsRecommendInfo->product_code);
        $result_search = $goodsHandler->searchGoodsSale($main_array);

        $goodsRecommendInfo->goodsName = $result_search['goodsName'];
        $goodsRecommendInfo->salePrice = $result_search['salePrice'];

        //查询详细信息
        $promotionHandler = new promotionHandler($G_PGLOB->DBCA);

        $params['recom_id'] = $_REQUEST['id'];

        $recommendDetail = $promotionHandler->searchGoodsRecommendDetail( $params );

        //组装数组
        $detailArr = array();

        if( $recommendDetail ){

            foreach ( $recommendDetail as $detail ){

                //创建临时数组
                $tempArr = array();

                $arg_array = array(	'product_code' 	=> $detail['product_code']);
                $result_search = $goodsHandler->searchGoodsSale($arg_array);

                $tempArr['product_code'] = $detail['product_code'];
                $tempArr['goodsName']    = $result_search['goodsName'];
                $tempArr['salePrice']    = $result_search['salePrice'];
                $tempArr['sale_price']    = $detail['sale_price'];

                $detailArr[] = $tempArr;

            }

        }

        $G_PGLOB->smarty->assign_by_ref( 'recommendGoodsInfo' , $detailArr );
        $G_PGLOB->smarty->assign_by_ref('goodsRecommendInfo' , $goodsRecommendInfo);
        $G_PGLOB->smarty->display('promotion/goodsRecommendDetail.tpl');

    }

}